<?php
/**
 * 工具函数包，系统加载的时候自动加载
 * 
 * @author Lee<a605333742@gmail.com>
 * @time    2016-07-24
 */

/*
 * 根据节点id获取节点名
 * 
 * @param   $node_id    节点id
 * 
 * @return  str $nodeName
 */
function getNodeName($node_id=''){
    if($node_id=="" ||$node_id==0){
        return "顶级节点";
    }
    //if($nodeName=S("nodeName")){
    //    return $nodeName[$node_id];
    //}
    $node       =D("Node");
    $nodeList   =$node->select(array('field' => 'id,node_name'));
    $data       =array();
    foreach($nodeList as $vo){
        $data[$vo['id']]=$vo['node_name'];
    }
    //S("nodeName",$data,3600*24);
    $nodeName   =$data[$node_id];    
    return $nodeName;
}


/*
 * 异位或加密和解密
 * 
 * @param   $value 需要加密的字符
 * @param   $type  操作类型，不存在为解密，默认为解密
 * 
 * @reurn   str $value处理过的字符串
 */
function encode($value,$type=''){
    //$type不存在为解密，默认为解密
    $key	=md5(C('AUTO_LOGIN_KEY'));
    if($type){
        //加密
        return str_replace("=",'',base64_encode($value ^ $key));
    }
    //解密
    $value	=base64_decode($value);
    return  $value ^ $key;
}

/*
 * 根据id获取用户名
 * 
 * @param   $userId 用户id
 * 
 * @return str  $usename
 */
function getUserName($u_id=''){
    if($u_id==''){
        return "";
    }
    //if($userName=S("username",$data)){
    //    return $userName[$u_id];
    //}
    $user       =D("User");
    $userList   =$user->select(array("field"=>"id,nickname"));
    $data       =array();
    foreach($userList as $vo){
        $data[$vo['id']]=$vo['nickname'];
    }
    S("username",$data,3600*12);
    $userName   =$data[$u_id];
    return $userName;
}

/*
 * 根据id获取地区名
 * 
 * @param   $regionId   地区id
 *
 * @return str  regionName
 */
function getRegion($regionId=''){
    if($regionId==''){
        return "";
    }
    //if($regionName=S("regionName")){
    //    return $regionName[$regionId];
    //}
    $region     =D("Region");
    $regionList =$region->select(array("file"=>"id,name"));
    $data       =array();
    foreach ($regionList as $vo){
        $data[$vo['id']]=$vo['name'];
    }
    S("regionName",$data,3600*12);
    $regionName =$data[$regionId];
    return $regionName;
}

/*
 * 根据用户id获取用户电话
 * 
 * @param   $userId 用户id
 * 
 * @return  str tel
 */
function getTel($userId=''){
    if($userId==''){
        return "";
    }
    //if($tel=S("tel")){
    //    return $tel[$userId];
    //}
    $user       =D("User");
    $userList   =$user->select(array("field"=>"id,tel"));
    $data       =array();
    foreach($userList as $vo){
        $data[$vo['id']]=$vo['tel'];
    }
    S("tel",$data,3600*12);
    $tel    =$data[$userId];
    return $tel;
}

/*
 * 根据用户id获取用户公司名称
 * 
 * @return str companyName
 */
function getCompany($user_id=''){
    if($user_id==''){
        return "";
    }
    $user       =D("User");
    $userList   =$user->select(array("field"=>"id,companyName"));
    foreach($userList as $vo){
        $data[$vo['id']]=$vo['companyname'];
    }
    $companName =$data[$user_id];
    return $companName;
}
    
/*
 * 根据用户id获取用户地址
 * 
 * @param   $userId 用户id
 * 
 * @return str address

function getAdd($userId=''){
    if($userId==''){
        return "";
    }
    if($address=S("address")){
        return $address[$userId];
    }
    $user       =D("User");
    $userList   =$user->select(array("field"=>"id,address"));
    $data       =array();
    foreach($userList as $vo){
        $data[$vo['id']]=$vo['address'];
    }
    S("address",$data,3600*12);
    $address    =$data[$userId];
    return $address;
}
 * 
 */

/*
 * 状态值
 * 
 * @param   $status 状态值
 * @param   $imageShow 图片
 * 
 * @return str  state
 */
function changeState($state,$id,$type){
    switch ($state) {
        case 2 :
            $info = '<a href="'.U(CONTROLLER_NAME.'/audit',array('id'=>$id,'type'=>$type)) . '" target="ajaxTodo">待审</a>';
            break;
        case 1 :
            $info = '<a href="'.U(CONTROLLER_NAME.'/pass',array('id'=>$id,'type'=>$type)) . '" target="ajaxTodo">批准</a>';
            break;
    }
    return $info;
}

/*
 * 根据状态显示相应的图片
 * 
 * @return  #
 */
function getStatus($status, $imageShow = true){
    switch ($status) {
        case 0 :
            $showText = '禁用';
            $showImg = '<IMG SRC="/tuyouApp/Application/Public/Images/locked.gif" style="height: 20px; width: 20px;" BORDER="0" ALT="禁用">';
            break;
        case 1 :
            $showText = '待审';
            $showImg = '<IMG SRC="/tuyouApp/Application/Public/Images/prected.gif" style="height: 20px; width: 20px;" BORDER="0" ALT="待审">';
            break;
        case 2 : 
            $showText = '正常';
            $showImg = '<IMG SRC="/tuyouApp/Application/Public/Images/ok.gif" style="height: 20px; width: 20px;" BORDER="0" ALT="正常">';
        break;
        default :
            $showText = '删除';
            $showImg = '<IMG SRC="/tuyouApp/Application/Public/Images/del.gif" style="height: 20px; width: 20px;" BORDER="0" ALT="删除">';
            break;    
    }
    return ($imageShow === true) ? $showImg : $showText;
}

/*
 * 根据type获取帖子类型
 * 
 * @param   $forumid  帖子类型id
 * @param   $forumtype 类型，1为涂医生论坛回帖，2为配方留言，3为招商信息留言，4为求购信息留言，5为供应信息留言',
 * 
 * @return str $forumName
 */
function getForum($forumid='',$forumtype=''){
    if($forumid==""){
        return "";
    }
    if($forumtype==1){
        $typeList   =D("Doctorquesttype")->select(array("field"=>"id,name"));
        $data       =array();
        foreach($typeList as $vo){
            $data[$vo['id']]=$vo['name'];
        }
        $forumName  =$data[$forumid];
        return $forumName;
    }else if($forumtype==2){
        $typeList   =D("Recipe")->select(array("field"=>"id,name"));
        $data       =array();
        foreach($typeList as $vo){
            $data[$vo['id']]=$vo['name'];
        }
        $forumName   =$data[$forumid];
        return $forumName;
    }else if($forumtype==3){
        $typeList   =D("Merchant")->select(array("field"=>"id,title"));
        $data       =array();
        foreach($typeList as $vo){
            $data[$vo['id']]=$vo['title'];
        }
        $forumName   =$data[$forumid];
        return $forumName;
    }else if($forumtype==4){
        $typeList   =D("Buy")->select(array("field"=>"id,title"));
        $data       =array();
        foreach($typeList as $vo){
            $data[$vo['id']]=$vo['title'];
        }
        $forumName   =$data[$forumid];
        return $forumName;
    }else if($forumtype==5){
        $typeList   =D("Supply")->select(array("field"=>"id,title"));
        $data       =array();
        foreach($typeList as $vo){
            $data[$vo['id']]=$vo['title'];
        }
        $forumName   =$data[$forumid];
        return $forumName;
    }   
}

/*
 * 根据id获取关联的帖子
 * 
 * @param $doc_id  涂医生id
 * 
 * @return  str $docName
 */
function getDoc($doc_id=''){
    if($doc_id==''){
        return "";
    }    
    $doc    =D("Doctor");
    $docList=$doc->select(array("field"=>"id,title"));
    $data   =array();
    foreach($docList as $vo){
        $data[$vo['id']]=$vo['title'];
    }
    //S("tel",$data,3600*12);
    $docName    =$data[$doc_id];
    return $docName;
}

/*
 * 根据岗位id获取岗位
 * 
 * @return str $jobName
 */
function getJob($job_id=''){
    if($job_id==''){
        return '';
    }
    $job    =D("RecruitsJobType");
    $jobList=$job->select(array("field"=>"jobId,jobName"));
    $data   =array();
    foreach($jobList as $vo){
        $data[$vo['jobid']] =$vo['jobname'];
    }
    $jobName=$data[$job_id];
    return $jobName;
}

/*
 * 根据薪水id获取薪水
 * 
 * @return str  $salaryName
 */
function getSalary($salary_id=''){
    if($salary_id==''){
        return '';
    }
    $salary     =D("RecruitsSalaryType");
    $salaryList =$salary->select(array("field"=>"salaryId,salaryName"));
    $data       =array();
    foreach($salaryList as $vo){
        $data[$vo['salaryid']]  =$vo['salaryname'];
    }
    $salaryName =$data[$salary_id];
    return $salaryName;
}

/*
 * 根据材料id获取材料名
 * 
 * @return str $materialName
 */
function getMaterial($material_id=''){
    if($material_id==''){
        return "";
    }
    $material       =D("Material");
    $materialList   =$material->select(array("field"=>'id,name'));
    $data           =array();
    foreach($materialList as $vo){
        $data[$vo['id']]=$vo['name'];
    }
    $materialName   =$data[$material_id];
    return $materialName;
}

/*
 * 根据配方id 获取配方名
 * 
 * @return str  $recipeName
 */
function getReciName($recipe_id=''){
    if($recipe_id==''){
        return "";
    }
    $recipe =D("Recipe");
    $recList=$recipe->select(array("field"=>"id,name"));
    $data   =array();
    foreach($recList as $vo){
        $data[$vo['id']]=$vo['name'];
    }
    $recipeName =$data[$recipe_id];
    return $recipeName;
}

/*
 * 根据新闻id获取新闻标题
 * 
 * @return str newTitle
 */
function getNewsTitle($newsId=''){
    if($newsId==""){
        return "";
    }
    $news       =D("News");
    $newsList   =$news->select(array("field"=>"id,title"));
    $data       =array();
    foreach($newsList as $vo){
        $data[$vo['id']]=$vo['title'];
    }
    $newsTitle  =$data[$newsId];
    return $newsTitle;
}

/*
 * 获取地区
 *
 * @param $area_id 地区编码
 *  
 * @return areaName
 */
function getAddr($area_id=''){
    if($area_id==''){
        return "";
    }
    $area       =D("Area");
    $areaList   =$area->select(array("id"=>"id,province,city,country"));
    $data       =array();
    foreach($areaList as $vo){
        $data[$vo['id']]=$vo['province'].$vo['city'].$vo['country'];
    }
    $areaName   =$data[$area_id];
    return $areaName;
}

/*
 * 涂讨论第一张图片处理
 * 
 * @param   $image  图片组字符串
 * @param   $type   第几张图片
 * 
 * @return  str $imgUrl
 */
function subImg($image,$type=''){
    if($image==''){
        return '';
    }
    if($type==1 || $type==''){
        if(stripos($image,',')){
            return substr($image,0,stripos($image,','));
        }else{
            return substr($image,0);
        }       
    }else if($type==2){
        $no2        =substr($image,stripos($image,',')+1);
        if(substr_count($image,'.')<2){
            return '';
        }else if(substr_count($image,'.')==2){
            return trim(substr($no2,0,stripos($no2,',')),',');
        }else if(substr_count($image,'.')>2){
            return substr($no2,0,stripos($no2,','));
        }
    }else if($type==3){
        if(substr_count($image,'.')<3){
            return '';
        }
        if(substr($image,-1)==','){
            $no3    =substr($image,0,-1);
            return substr($no3,strrpos($no3,',')+1);
        }else{
            return substr($image,strrpos($image,',')+1);
        }
    }
}




